﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ErrorCode;
using SmartHealthcare.Write.Api.Application.Command.Case;
using SmartHealthcare.Write.Api.Application.Command.Department;
using SmartHealthcare.Write.Api.Application.Command.HospitalBed;
using SmartHealthcare.Write.Api.Application.Command.InpatientRegistry;
using SmartHealthcare.Write.Api.DTo;

namespace SmartHealthcare.Write.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SmartHealthcareController : ControllerBase
    {
        private readonly IMediator mediator;

        public SmartHealthcareController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 创建科室
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> CreateDepartment(CreateDepartmentCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 添加病例
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<APIResult<int>> CreateCase(CreateCaseCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除病例
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<APIResult<int>> DeleteCase(DeleteCaseCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 删除科室
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<APIResult<int>> DeleteDepartmentC(DeleteDepartmentCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 更新科室信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<APIResult<int>> UpdateDepartment(UpdateDepartmentCommand request)
        {
            return mediator.Send(request);
        }
        /// <summary>
        /// 创建病房和病床
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> CreateHospitalBed([FromBody] CreateHospitalBedCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 创建住院登记
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> CreateInpatientRegistry([FromBody] CreateInpatientRegistryCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 更新病人预交金额
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateAdvancePayment([FromBody] UpdateAdvancePaymentCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 出院结算
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<DischargeSettlementResultDto>> DischargeSettlement([FromBody] DischargeSettlementCommand request, CancellationToken cancellationToken)
        {
            return await mediator.Send(request, cancellationToken);
        }

        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<string> TestDatabaseConnection()
        {
            var res = new APIResult<string>();
            try
            {
                // 获取数据库连接字符串
                var connectionString = HttpContext.RequestServices.GetService<IConfiguration>()?.GetConnectionString("MySQL");
                
                if (string.IsNullOrEmpty(connectionString))
                {
                    res.Code = APIEnums.Fail;
                    res.Msg = "数据库连接字符串为空";
                    res.Data = "请检查appsettings.json中的ConnectionStrings配置";
                    return res;
                }

                res.Code = APIEnums.Success;
                res.Msg = "数据库连接字符串配置正常";
                res.Data = $"连接字符串: {connectionString}";
                return res;
            }
            catch (Exception ex)
            {
                res.Code = APIEnums.Fail;
                res.Msg = "数据库连接测试失败";
                res.Data = $"错误信息: {ex.Message}";
                return res;
            }
        }

    }
}
